<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/17 0017
 * Time: 下午 11:01
 */

namespace Home\Controller;


class MallController extends CheckController
{
    /**
     * 商品列表
     */
    public function index()
    {
        $goods = M('goods');
        $list = M()->query('SELECT
                                        a.content,
                                        a.`name`,
                                        a.id,
                                        a.money,
                                        a.integral,
                                        b.path
                                    FROM
                                        onethink_goods AS a
                                    LEFT JOIN onethink_goodspic AS b ON a.id = b.g_id
                                    WHERE
                                    a.isshow = 1');

        $return = array();
        foreach ($list as $key => $value) {
            $return[$value['id']] = $value;
        }
        foreach ($return as $key => $value) {
            if ($value['path'] == null) {
                $return[$key]['path'] = '/default.jpg';
            }
        }
        $this->assign('_list',$return);
        $this->display();
    }

    /**
     * 单个商品
     */
    public function one()
    {
        $id = (int)I('id');
        $goods = M('goods');
        $goodspic = M('goodspic');
        $info = $goods->find($id);
        $pic = $goodspic->where('g_id='.$id)->select();
        $this->assign('info',$info);
        $this->assign('pic',$pic);
        $this->display();
    }

    /**
     * pay 支付
     */
    public function pay()
    {
        $type = I('type');
        $gid = (int)I('gid');
        $goods = M('goods');
        $treemember = M('treemember');
        $goodsorder = M('goodsorder');
        $integralchange = M('integralchange');
        $goods_info = $goods->find($gid);
        if ($goods_info === null) {
            $this->ajaxReturn(array('info'=>'商品无效或商品已售空!','reg'=>3),'json');
        }
        $openid = session('openid');
        $per_info = $treemember->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
        if ($goods_info['integral'] > $per_info['integral'] && $type == 'integral') {
            $this->ajaxReturn(array('info'=>'您的积分不足，请选择现金购买!','reg'=>4),'json');
        }
        if ($type == 'money') {
            $res = $goodsorder->data(array('money'=>$goods_info['money'],'g_id'=>$gid,'type'=>2,'createtime'=>time(),'uid'=>$per_info['id']))->add();
            if ($res === false) {
                $this->ajaxReturn(array('info'=>'购买失败!','reg'=>2),'json');
            } else {
                $this->ajaxReturn(array('info'=>'购买成功!','reg'=>1),'json');
            }
        } else {
            M()->startTrans();
            //增加订单信息
            $res = $goodsorder->data(array('integral'=>$goods_info['integral'],'g_id'=>$gid,'type'=>1,'createtime'=>time(),'uid'=>$per_info['id']))->add();
            if($res === false) {
                M()->rollback();
            }
            //会员减分
            $res_member = $treemember->where(array('id'=>$per_info['id']))->setDec('integral',$goods_info['integral']);
            if ($res_member === false) {
                M()->rollback();
            }
            $res_integralchange = $integralchange->data(array('uid'=>$per_info['id'],'type'=>1,'integral'=>'-'.$goods_info['integral'],'changetime'=>time()))->add();
            if ($res_integralchange === false) {
                M()->rollback();
            }
            //提交事务
            M()->commit();
            $this->ajaxReturn(array('info'=>'购买成功!','reg'=>1),'json');
        }
    }
    
    /**
     * 提现
     */
    public function apply()
    {
        $openid = session('openid');
        $treemember = D('treemember');
        $info = $treemember->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
        if (IS_POST) {
            $exchange = M('exchange');
            $integralchange = M('integralchange');
            $integral = (int)I('integral');
            if ($info['integral'] < $integral) {
                $this->ajaxReturn(array('info'=>'您的积分不足，不能兑换!','reg'=>2),'json');
            }
            if ($integral == 0) {
                $this->ajaxReturn(array('info'=>'最少兑换1积分!','reg'=>2),'json');
            }
            M()->startTrans();
            /**减分**/
            $res = $treemember->where('id='.$info['id'])->setDec('integral',$integral);
            if ($res === false) {
                M()->rollback();
            }
            /**写入提现表**/
            $res_exchange = $exchange->data(array('uid'=>$info['id'],'createtime'=>time(),'integral'=>$integral,'money'=>$integral*C('exchange')))->add();
            if ($res_exchange === false) {
                M()->rollback();
            }
            /**写入积分变更表**/
            $res_integralchange = $integralchange->data(array('uid'=>$info['id'],'type'=>2,'integral'=>'-'.$integral,'changetime'=>time()))->add();
            if ($res_integralchange === false) {
                M()->rollback();
            }
            M()->commit();
            $this->ajaxReturn(array('info'=>'兑换成功!','reg'=>1),'json');
        } else {
            $this->assign('_info',$info);
            $this->assign('money',$info['integral'] * C('exchange'));
            $this->display();
        }
    }
}